#include "orangepch.h"
#include "OpenGLBuffer.h"
#include "glad/glad.h"

namespace Orange {
	OpenGLVertexBuffer::OpenGLVertexBuffer(uint32_t size)
	{
		/// <summary>
		/// ������ ʱ �ᴫ�� ���㻺�� �Ĵ�С ��Ȼ��һ֡һ֡ ��Ⱦ
		/// </summary>
		/// <param name="size"></param>
		glCreateBuffers(1, &m_RendererId);

		glBindBuffer(GL_ARRAY_BUFFER, m_RendererId);
		glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
	}
	/// <summary>
	///  ���� opengl ʵ�ֵĶ��㻺��
	/// </summary>
	/// <param name="vertexs"></param>
	/// <param name="size"></param>
	OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertexs, uint32_t size)
	{

		glCreateBuffers(1, &m_RendererId);

		glBindBuffer(GL_ARRAY_BUFFER, m_RendererId);
		glBufferData(GL_ARRAY_BUFFER, size, vertexs, GL_STATIC_DRAW);

	}
	OpenGLVertexBuffer::~OpenGLVertexBuffer()
	{
		glDeleteBuffers(1, &m_RendererId);
	}
	void OpenGLVertexBuffer::bind() const
	{
		glBindBuffer(GL_ARRAY_BUFFER, m_RendererId);
	}
	void OpenGLVertexBuffer::ubind() const
	{
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	void OpenGLVertexBuffer::setLayout(BufferLayout& layout)
	{
		m_layout = layout;
	}

	BufferLayout OpenGLVertexBuffer::getLayout() const
	{
		return m_layout;
	}

	void OpenGLVertexBuffer::SetData(void* data, uint32_t size)
	{
		glBindBuffer(GL_ARRAY_BUFFER, m_RendererId);
		glBufferSubData(GL_ARRAY_BUFFER,0,size,data);
	}

	/// <summary>
	///  ���� opengl ʵ�ֵ���������
	/// </summary>
	/// <param name="vertexs"></param>
	/// <param name="size"></param>
	OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indexs, uint32_t count):m_count(count)
	{

		glCreateBuffers(1, &m_RendererId);

		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererId);

		glBufferData(GL_ELEMENT_ARRAY_BUFFER, count*sizeof(uint32_t), indexs, GL_STATIC_DRAW);
	}

	OpenGLIndexBuffer::~OpenGLIndexBuffer()
	{
		glDeleteBuffers(1, &m_RendererId);
	}

	void OpenGLIndexBuffer::bind() const
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererId);

	}

	void OpenGLIndexBuffer::ubind() const
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	}

	uint32_t OpenGLIndexBuffer::getCount() const
	{
		return m_count;
	}


}